package com.zl.learning.bigdata.zookeeper;

import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class TestZookeeper2 {
    private ZooKeeper zk;

    //    @Before
    public void before() throws IOException {
        zk = new ZooKeeper("172.31.4.247:2181", 2000, event -> {
            // 获取事件的状态
            Watcher.Event.KeeperState keeperState = event.getState();
            Watcher.Event.EventType eventType = event.getType();
            // 如果是建立连接
            if (Watcher.Event.KeeperState.SyncConnected == keeperState) {
                if (Watcher.Event.EventType.None == eventType) {
                    // 如果建立连接成功，则发送信号量，让后续阻塞程序向下执行
                    System.out.println("zk 建立连接");
                }
            }
            System.out.println(keeperState);
            System.out.println(eventType);
            switch (eventType) {
                case NodeDeleted: {
                    System.out.println("服务下线了：" + event.getPath());
                    break;
                }
                case NodeCreated: {
                    System.out.println("服务上线了：" + event.getPath());
                    break;
                }
//                case NodeChildrenChanged: {
//                    System.out.println("服务上线了：" + event.getPath());
//                    break;
//                }
                case NodeDataChanged: {
                    System.out.println("服务地址改变了：" + event.getPath());
                    break;
                }
                default:
                    break;
            }
        });
    }

    @Test
    public void test1() throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        System.out.println("Local HostAddress: " + addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: " + hostname);
    }

    @Test
    public void test2() {
        List<String> ipList = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface;
            Enumeration<InetAddress> inetAddresses;
            InetAddress inetAddress;
            String ip;
            while (networkInterfaces.hasMoreElements()) {
                networkInterface = networkInterfaces.nextElement();
                inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                        ip = inetAddress.getHostAddress();
                        ipList.add(ip);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        System.out.println(ipList);
    }
}
